package com.pay.utils;

import com.pay.annotation.Controller;
import com.pay.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

/**
 * 获取类文件帮助类
 * Created by Administrator on 2017-7-7.
 */
public class ClassHelper {

    private static final Logger logger = LoggerFactory.getLogger(ClassHelper.class);
    private static Set<Class<?>> CLASS_SET;

    static {
        String basePackagePath = ConfigUtils.getProperty(Constants.APP_BASE_PACKAGE);
        CLASS_SET = ClassUtils.getClassSet(basePackagePath);
    }

    /**
     * 获取根目录下所有CLASS类
     * @return
     */
    public static Set<Class<?>> getClassSet() {
        return CLASS_SET;
    }

    /**
     * 获取所有service类
     * @return
     */
    public static Set<Class<?>> getServiceClassSet(){
        Set<Class<?>> serviceClassSet = new HashSet<>();
        for(Class cls : CLASS_SET){
            if(cls.isAnnotationPresent(Service.class)){
                serviceClassSet.add(cls);
            }
        }
        return serviceClassSet;
    }

    /**
     * 获取所有Controller类
     * @return
     */
    public static Set<Class<?>> getControllerClassSet(){
        Set<Class<?>> ctlClassSet = new HashSet<>();
        for(Class cls : CLASS_SET){
            if(cls.isAnnotationPresent(Controller.class)){
                ctlClassSet.add(cls);
            }
        }
        return ctlClassSet;
    }

    /**
     * 获取所有Service,Controller类的bean
     * @return
     */
    public static Set<Class<?>> getBeanClassSet(){
        Set<Class<?>> beanClassSet = new HashSet<>();
        beanClassSet.addAll(getServiceClassSet());
        beanClassSet.addAll(getControllerClassSet());
        return beanClassSet ;
    }

}
